import axios from 'axios';
import router from '@/router/index';
import reConfig from '@/reConfig/index';
import store from '@/store/index';
import Vue from 'vue';
import qs from 'qs';

/**
 * @method: axios 二次封装
 * @params: prefix = 请求域名前缀符(or API版本号)；config = 请求配置，目前支持loading，请求头Content-Type
 */
export default function (prefix = reConfig.apiPrefix, config = {}) {
    let {
        loading
    } = config;

    //忽略序列化列表
    let ignoreQs = ['multipart/form-data', 'application/json'];

    // 创建一个axios实例 config、prefix 为独立请求的配置
    const INSTANCES = _axiosConfig(prefix, config);
    // request 拦截器
    INSTANCES.interceptors.request.use(
        config => {
            if (loading) store.commit('setGlobalLoading', true);
            return config;
        },
        function (error) {
            return Promise.reject(error);
        }
    );

    // response 拦截器
    INSTANCES.interceptors.response.use(
        function (response) {
            let {
                data
            } = response;
            if (data && data.status === 401) {
                Vue.prototype.$message.error(data.msg);
                sessionStorage.clear()
                router.push({
                    name: "login"
                  });
                return;
            }
            if (data && data.status === 400) {
                store.commit('setGlobalLoading', false);
                Vue.prototype.$message.error(data.msg);
                return Promise.reject(response.data)
            }
            // 错误提示
            if (response.data.msg && response.data.status !== 200) {
                console.log('showMessage');
            }
            store.commit('setGlobalLoading', false);
            return response.data;
        },
        function (error) {
            store.commit('setGlobalLoading', false);
            Vue.prototype.$message.error('请求错误，请稍后重试');
            return Promise.reject(error);
        }
    );

    return {
        // get 请求将参数加进 config[params]
        get: (url, params, config = {}) => {
            config.params = params;
            return INSTANCES.get(url, config);
        },
        // post 请求序列化 params
        post: (url, params) => {
            //兼容上传文件
            if (ignoreQs.indexOf(config['Content-Type']) === -1) {
                params = qs.stringify(params);
            }
            return INSTANCES.post(url, params);
        },
        put: INSTANCES.put,
        patch: INSTANCES.patch
    };
}

// 重设config
function _axiosConfig(prefix, config) {
    // 默认请求头
    const DEFAULT_HEADER = {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    };
    // 需要更改请求头Content-Type 信息，在接口定义处配置config 参数: Content-Type
    axios.defaults.withCredentials = true;
    // 开发环境默认使用proxy代理请求
    axios.defaults.baseURL = process.env.NODE_ENV === 'development'? `/proxyApi/${prefix}` : `${prefix}`;
    // axios.defaults.baseURL = 'http://www.serve.com';
    axios.defaults.timeout = 30000;
    config = {
        'headers': {
            'Content-Type': config['Content-Type'] || DEFAULT_HEADER['Content-Type'],
            'access-token': sessionStorage.getItem('token')
        },
        ...config
    };
    return axios.create(config);
}